﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using DBConnectionLib;

namespace DBSourceToolsLib
{
    public class ScriptRunner : ProcessRunner
    {
        private string m_ScriptName;
        private string m_ScriptSQL;
        private string m_TempScriptFileName;
        //private string m_TempOutputFileName;
        DBConnectionLib.Database m_Database;
        public ScriptRunner(Database oDatabase, bool returnRunningTime)
            : base ( returnRunningTime)
        {
            this.m_Database = oDatabase;
            this.m_TempScriptFileName = System.IO.Path.GetTempFileName();

        }

        public override string GetWorkingDir()
        {
            return null;
        }
        public override string GetFileName()
        {
            if (!System.String.IsNullOrEmpty(this.m_Database.CommandSQL))
                return this.m_Database.CommandSQL;
            else
                return (UserSettings.Instance().UseIsql ? "isql.exe" : "sqlcmd.exe");
            //return "";
            
        }
        public override string GetArguments()
        {
            string strLoginOptions = " -E ";
            if (!string.IsNullOrEmpty(this.m_Database.UserName))
            {
                strLoginOptions = " -U " + this.m_Database.UserName + " -P " + this.m_Database.Password;
            }

            string strArgs = " -S " + this.m_Database.DataSourceName + strLoginOptions + " -d \"" + this.m_Database.DatabaseName + "\""
                + " -i \"" + this.m_TempScriptFileName + "\""
                + (this.UsingStreams ? "" : (" -o " + this.GetOutputFileName() + " -u") );
            return strArgs;
            //return "";
        }

        public override string GetOutputFileName()
        {
            return ".\\temp\\" + this.m_ScriptName + "results.txt";
        }

        public void RunScriptUsingFile(string strQueryName, string sqlScript)
        {
            this.m_ScriptName = strQueryName;
            this.m_ScriptSQL = sqlScript;
            base.RunUsingFile();
        }

        public void RunScriptUsingStreams(string strScriptName, string strScript)
        {
            // create script.
            FileUtils.EnsureFileIsWriteable(this.m_TempScriptFileName);
            using (System.IO.StreamWriter oWriter = new System.IO.StreamWriter(this.m_TempScriptFileName))
            {
                oWriter.Write(RemoveLineFeeds(strScript));
            }
            //this.m_ScriptName = strScriptName;
            //this.m_ScriptSQL = strScript;
            base.RunUsingStreams();
        }

        string RemoveLineFeeds(string strInput)
        {
            char[] linefeed = { '\r' };
            return string.Join("", strInput.Split(linefeed));
        }

        //public override string GetSQLScript()
        //{
        //    return this.m_ScriptSQL;
        //}
        //public override string GetScriptName()
        //{
        //    return this.m_ScriptName;
        //}

        public string GetResultsText()
        {
            string strResults = "";
            if (System.IO.File.Exists(this.GetOutputFileName()))
            {
                using (System.IO.StreamReader oStreamReader = new System.IO.StreamReader(
                    this.GetOutputFileName()))
                {
                    strResults += oStreamReader.ReadToEnd();
                }
            }
            return strResults;

        }

        public override string GetTempFileName()
        {
            return m_TempScriptFileName;
        }
    }
}
